package com.platform.module.common.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;


public class MD5Helper {

    public static final String MD5_KEY = "iysEV6rv#p0NCnE9";
    private static final String HEX_CHARS = "0123456789abcdef";

    private MD5Helper() {
        throw new UnsupportedOperationException("Instantiation operation is not supported.");
    }

    static MessageDigest getDigest() {
        try {
            return MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException var1) {
            throw new RuntimeException(var1);
        }
    }

    public static byte[] md5(byte[] data) {
        return getDigest().digest(data);
    }

    public static byte[] md5(String data) {
        return md5(data.getBytes());
    }

    public static String md5Hex(byte[] data) {
        return toHexString(md5(data));
    }

    public static String md5Hex(String data) {
        return md5Hex(data, null);
    }

    public static String md5Hex(String data, String privateKey) {
        if (privateKey == null) {
            privateKey = MD5_KEY;
        }
        return toHexString(md5(data + privateKey));
    }

    public static String md5NoKey(String data) {
        return toHexString(md5(data));
    }

    public static String sign(String data, String key) {
        return toHexString(md5(data + key));
    }

    public static String toHexString(byte[] b) {
        StringBuilder sb = new StringBuilder();

        for (byte value : b) {
            sb.append(HEX_CHARS.charAt(value >>> 4 & 15));
            sb.append(HEX_CHARS.charAt(value & 15));
        }

        return sb.toString();
    }

    public static byte[] toByteArray(String s) {
        byte[] buf = new byte[s.length() / 2];
        int j = 0;

        for (int i = 0; i < buf.length; ++i) {
            buf[i] = (byte) (Character.digit(s.charAt(j++), 16) << 4 | Character.digit(s.charAt(j++), 16));
        }

        return buf;
    }

    public static String appendParam(String returnStr, String paramId, String paramValue) {
        if (!returnStr.equals("")) {
            if (!paramValue.equals("")) {
                returnStr = returnStr + "&" + paramId + "=" + paramValue;
            }
        } else if (!paramValue.equals("")) {
            returnStr = paramId + "=" + paramValue;
        }
        return returnStr;
    }

    public static String appendParam_all(String returnStr, String paramId, String paramValue) {
        if (!returnStr.equals("")) {
            returnStr = returnStr + "&" + paramId + "=" + paramValue;
        } else {
            returnStr = paramId + "=" + paramValue;
        }
        return returnStr;
    }
}
